package com.twohand.admin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.twohand.admin.Dto.MenuAddDto;
import com.twohand.admin.Entity.Menu;
import com.twohand.admin.Mapper.MenuMapper;
import com.twohand.admin.Service.MenuService;
import com.twohand.admin.Vo.MenuParentOptions;
import com.twohand.admin.Vo.SelectOptions;
import com.twohand.utils.Response.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Integer> getMenuIdList(Integer id) {
        //查出有权限使用的父级id
        return menuMapper.getAllMenuId(id);
    }

    @Override
    public List<String> getPermissionByUserId(Integer id) {
        return menuMapper.getOprationMenu(id);
    }

    @Override
    public List<Menu> getMenuByParentId(Integer parentId) {
        return menuMapper.getMenuByParentId(parentId);
    }

    @Override
    public List<Menu> getMenuByParentId(Integer parentId, List<Integer> menuIdList) {
        //查出的是子级菜单
        List<Menu> menuList = getMenuByParentId(parentId);
        if(menuIdList == null){
            return menuList;
        }
        List<Menu> userMenuList = new ArrayList<>();
        for(Menu menu : menuList){
            if(menuIdList.contains(menu.getId())){
                // 包含改子菜单
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<Menu> getTreeMenuList(List<Menu> menuList, List<Integer> menuIdList) {
        //查出有权限的id
        List<Menu> subMenuList = new ArrayList<Menu>();
        for(Menu menu :menuList){
            if(menu.getType() == 0){
                menu.setChildren(getTreeMenuList(getMenuByParentId(menu.getId(),menuIdList),menuIdList));
            }
            subMenuList.add(menu);
        }
        return subMenuList;
    }

    @Override
    public List<Menu> getDeepTreeMenuList(List<Menu> menuList, List<Integer> menuIdList) {
        //查出有权限的id
        List<Menu> subMenuList = new ArrayList<Menu>();
        for(Menu menu :menuList){
            if(menu.getType() != 2){
                menu.setChildren(getDeepTreeMenuList(getMenuByParentId(menu.getId(),menuIdList),menuIdList));
            }
            subMenuList.add(menu);
        }
        return subMenuList;
    }


    @Override
    public List<Menu> getUserMenuList(Integer id) {
        //1,根据userid 查出有效的路径id
        List<Integer> menuIdList = getMenuIdList(id);
        List<Menu> menuList = getMenuByParentId(0,menuIdList);
        //调用递归
        getTreeMenuList(menuList,menuIdList);
        return menuList;
    }

    @Override
    public List<Menu> getBaseMenu() {
        //获取所有menu id
        List<Integer> menuIdList = menuMapper.queryAllMenuId();
        List<Menu> menuList = getMenuByParentId(0,menuIdList);
        getDeepTreeMenuList(menuList,menuIdList);
        return menuList;
    }

    @Override
    public List<MenuParentOptions> getParent() {
        return menuMapper.getOptions();
    }

    @Override
    public List<SelectOptions> getMenuType() {
        List<SelectOptions> menuType = new ArrayList<>();
        SelectOptions folder = new SelectOptions("目录",0);
        SelectOptions page = new SelectOptions("页面",1);
        SelectOptions button = new SelectOptions("按钮",2);
        menuType.add(folder);
        menuType.add(page);
        menuType.add(button);
        return menuType;
    }

    @Override
    public Boolean addMenu(Menu menu) {
        return menuMapper.insert(menu) > 0;
    }

    @Override
    public Boolean updateMenu(Menu menu) {
        return menuMapper.updateById(menu) > 0;
    }

    @Override
    public Boolean delMenu(Integer id) {
        //删除的时候菜单不能有子元素，即pid 没有等于 id的
        Integer count = menuMapper.getCountPidById(id);
        if(count != 0){
            throw new BaseException(403,"当前菜单存在子菜单，不可删除！");
        }
        return menuMapper.deleteById(id) > 0;
    }


}
